package crmdna.common;

import crmdna.common.api.APIException;
import crmdna.common.api.APIResponse.Status;

public class AssertUtils {
  public static void ensure(boolean condition) {
    ensure(condition, "Pre condition failure");
  }

  public static void ensure(boolean condition, String messageIfError) {
    if (!condition)
      throw new APIException().status(Status.ERROR_RESOURCE_INCORRECT).message(
          messageIfError + " (AssertUtils.assertTrue failed)");
  }

  public static void ensureNotNull(Object obj, String messageIfError) {
    if (obj == null)
      throw new APIException().status(Status.ERROR_RESOURCE_INCORRECT).message(
          messageIfError + " (AssertUtils.assertTrue failed)");
  }

  public static void ensureNotNull(Object obj) {
    ensureNotNull(obj, "Found null object");
  }

  public static void ensureEqual(int expected, int actual, String messageIfError) {

    if (expected != actual) {
      String errMessage =
          "Expected [" + expected + "] actual [" + actual + "] (AssertUtils.assertEquals failed)";

      if (messageIfError != null) {
        errMessage = messageIfError + ". " + errMessage;
      }

      throw new APIException().status(Status.ERROR_RESOURCE_INCORRECT).message(errMessage);
    }
  }

  public static void ensureEqual(int expected, int actual) {

    ensureEqual(expected, actual, null);
  }
}
